/*

    This file is part of GLua

    GLua Development Environment
    Copyright (C) 2007 VoiDeD

    GLua is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    GLua is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using System.Windows.Forms;

namespace GLua
{
    /// <summary>
    /// Legacy class, was used during beta testing
    /// </summary>
    public class CRCChecker
    {
        public static void Check()
        {
            FileStream fs = new FileStream( Application.ExecutablePath, FileMode.Open, FileAccess.Read );
            SHA1 sha = SHA1.Create();

            byte[] data = new byte[fs.Length - 20];
            fs.Read( data, 0, ( int )fs.Length - 20 );

            byte[] b = sha.ComputeHash( data );
            bool valid = true;
            for ( int i = 0 ; i < 20 ; i++ )
                valid &= fs.ReadByte() == b[i];

            fs.Close();

            if ( !valid )
            {
                fs.Close();
                b[21] = 0; // sneaky crash
                Application.Exit();
            }
        }
    }
}
